<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <link rel="stylesheet" type="text/css" href="../css/icons.css" />
    <script src="../script/jquery-1.9.1.min.js"></script>
    <script src="../script/bootstrap.min.js"></script>
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            客户抱怨提报
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">

        <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
            <ul class="aui-list aui-form-list" style="margin-top: 1%;overflow-y: auto; ">
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label" style="width:250px">信息/问题时间</div>
                        <div class="aui-list-item-input" onclick="timePicker();">
                            <input type="text" placeholder="选择日期" class="aui-input" id="informationTime">
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label" style="width:250px">截至时间</div>
                        <div class="aui-list-item-input" onclick="timePicker2();">
                            <input type="text" placeholder="选择日期" class="aui-input" id="ext06">
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label" style="width: 250px">客户</div>

                        <div class="aui-list-item-input">
                            <input type="text" placeholder="客户" id="client" />
                        </div>
                    </div>
                </li>

                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label" style="width: 250px">问题发生环节</div>

                        <div class="aui-list-item-input">
                            <input type="text" placeholder="问题发生环节" id="problemOccurs" />
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label" style="width: 250px">生产单位</div>

                        <div class="aui-list-item-input">
                            <input type="text" placeholder="生产单位" id="productionUnits" />
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label" style="width: 250px">产品名称</div>

                        <div class="aui-list-item-input">
                            <input type="text" disabled placeholder="产品名称" id="mrlName" />
                        </div>
                        <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectMrlName()"></i>
                    </div>
                </li>

                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label" style="width: 250px">产品图号</div>

                        <div class="aui-list-item-input">
                            <input type="text" disabled placeholder="产品图号" id="mrlTuhao" />
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label" style="width: 250px">产品状态</div>

                        <div class="aui-list-item-input">
                            <input type="text" placeholder="产品状态" id="mrlStatus" />
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label" style="width: 250px">生产批次</div>

                        <div class="aui-list-item-input">
                            <input type="text" placeholder="生产批次" id="lotCode" />
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label" style="width: 250px">问题描述</div>

                        <div class="aui-list-item-input">
                            <input type="text" placeholder="问题描述" id="problemDescription" />
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label" style="width: 250px">问题数量</div>

                        <div class="aui-list-item-input">
                            <input type="text" placeholder="问题数量" id="problemNum" />
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label" style="width: 250px">问题原因类型</div>

                        <div class="aui-list-item-input">
                            <input type="text" placeholder="问题原因类型" id="problemCause" />
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label" style="width: 250px">部门</div>

                        <div class="aui-list-item-input">
                            <select id="ext03" placeholder="请选择部门" style="font-size: 14px;">
                            </select>
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label" style="width: 250px">质量工程师</div>

                        <div class="aui-list-item-input">
                            <input type="text" disabled placeholder="质量工程师" id="qualityEngineer" />
                        </div>
                        <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectUser()"></i>
                    </div>
                </li>
                <div class="aui-content" style="background-color: #ffffff">
                    <ul class="aui-list-view aui-grid-view" id="imgslist">
                        <li class="aui-list-view-cell aui-img aui-col-xs-4 aui-padded-5" id="addpic"
                            onclick="imgClick()">
                            <img class="aui-img-object" src="../image/add.jpg">
                        </li>
                    </ul>
                </div>
            </ul>
            <div style="display: flex;
                         justify-content: space-around;
                         padding-top: 10px;
                         padding-bottom: 10px;">
                <div class="aui-btn aui-btn-info" style="width:30%;margin-top:2%;" onclick="closeWin()">取消
                </div>
                <div class="aui-btn aui-btn-info" style="width:30%;margin-top:2%;" onclick="changeState()">确定
                </div>
            </div>
        </div>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/hammer.min.js"></script>
<script type="text/javascript" src="../script/clickThrottle.js"></script>
<script type="text/javascript">

    //光标所在id
    var idSelector = "";
    var qualityEngineerCode = "";
    var uiMediaScanner = null;
    var imageFilter = null;
    var imageBrowser = null;
    var ids = "";
    var ext02 = "";//责任人编码
    //初始化
    apiready = function () {
        // 引入多选模块
        uiMediaScanner = api.require('UIMediaScanner');
        // 引入过滤压缩模块
        imageFilter = api.require("imageFilter");
        // 引入图片浏览模块
        imageBrowser = api.require('imageBrowser');
        ids = api.pageParam.ids;
        console.log(ids)
        $("#informationTime").focus();
        idSelector = "informationTime";
        //部门下拉框
        doSearchDept()
    }

    // 添加长按方法
    function addPress(obj, index) {
        // 获取目前长按的对象
        var hammertime = new Hammer(obj[0]);
        // 绑定长按对象
        hammertime.on("press", function (e) {
            api.confirm({
                title: '温馨提示',
                msg: '您确定要删除该图片吗？',
                buttons: ['确定', '取消']
            }, function (ret, err) {
                if (ret.buttonIndex == 1) {
                    // 移除自己
                    $(obj).remove();
                    api.toast({
                        msg: '删除成功！'
                    });
                }
            });
        });
    }

    function goSelectUser() {
        api.openWin({
            name: 'mdEmployeeSelectNew',
            url: 'mdEmployeeSelectNew.html',
            pageParam: {
                name: "qckmmanagesubmitdetail"
            }
        });
    }

    function setUser(code, name) {
        $("#qualityEngineer").val(name);
        qualityEngineerCode = code
    }
    function imgClick() {
        // 获取图片宽高
        var normalW = $("#addpic img").width();
        api.actionSheet({
            title: '选择图片来源',
            buttons: ['照相机', '浏览相册']
        }, function (ret, err) {
            var index = ret.buttonIndex;
            switch (index) {
                // 拍照
                case 1:
                    // 打开拍照
                    api.getPicture({
                        sourceType: "camera",
                        encodingType: "jpg",
                        destinationType: "url",
                        mediaValue: "pic",
                        quality: 50,
                        saveToPhotoAlbum: true
                    }, function (ret, err) {
                        if (ret && ret.data) {
                            // 拍照返回的本地路径
                            var returnUrl = ret.data;
                            // 图片压缩
                            imgCompress(returnUrl, 0.5, 0.5, getExt(returnUrl), function (compressImg) {
                                var showImgHtml = "<li class='aui-list-view-cell aui-img aui-col-xs-4  aui-padded-5' style='margin-left: 5px;width:" + normalW + "px;height:" + normalW + "px;'><img class='aui-img-object' src='" + compressImg +
                                    "' style='width:" + normalW + "px;height:" + normalW + "px;'></li>";
                                // 追加图片
                                $("#addpic").before(showImgHtml);
                                // ########################################  绑定长按事件 ########################
                                addPress($("#imgslist img[src='" + compressImg + "']").parent("li"));

                                //上传图片
                                uploadFile(compressImg);
                            });
                        } else {
                            api.toast({
                                msg: '没有选择，或者选择出错'
                            });
                        }
                    });
                    break;
                // 打开图片选择器
                case 2:
                    uiMediaScanner.open({
                        type: 'picture',
                        column: 4,
                        classify: true,
                        max: 5,
                        sort: {
                            key: 'time',
                            order: 'desc'
                        },
                        texts: {
                            stateText: '已选*项',
                            cancelText: '取消',
                            finishText: '完成'
                        },
                        styles: {
                            bg: '#fff',
                            mark: {
                                icon: '',
                                position: 'bottom_left',
                                size: 20
                            },
                            nav: {
                                bg: '#b23e4b',
                                stateColor: '#fff',
                                stateSize: 18,
                                cancelBg: 'rgba(0,0,0,0)',
                                cancelColor: '#fff',
                                cancelSize: 18,
                                finishBg: 'rgba(0,0,0,0)',
                                finishColor: '#fff',
                                finishSize: 18
                            }
                        }
                    }, function (ret) {
                        if (ret) {
                            if (ret.list) {
                                for (var i = 0; i < ret.list.length; i++) {
                                    console.log(i)
                                    var selectImg = ret.list[i];
                                    // 获取图片的路径
                                    var selectimgPath = selectImg.path;
                                    var selectimgThumbPath = selectImg.thumbPath;
                                    // 图片压缩
                                    imgCompress(selectimgPath, 0.5, 0.5, selectImg.suffix, function (compressImg) {
                                        var showImgHtml = "<li id='test' class='aui-list-view-cell aui-img aui-col-xs-4 aui-padded-5' style='margin-left: 5px;width:" + normalW + "px;height:" + normalW + "px;'><img class='aui-img-object' src='" + compressImg +
                                            "' style='width:" + normalW + "px;height:" + normalW + "px;'></li>";
                                        // 追加图片
                                        $("#addpic").before(showImgHtml);
                                        // ########################################  绑定长按事件 ########################
                                        addPress($("#imgslist img[src='" + compressImg + "']").parent("li"));

                                        // ################### 上传图片 #########################
                                        uploadFile(compressImg);
                                    });
                                }
                            }
                        }
                    });
                    break;
            }
        });
    }

    function imgCompress(imgsrc, quality, scale, ext, callback) {
        // 压缩文件的保存目录
        var savePath = api.cacheDir + "/" + getNowFormatDate() + "/";
        // 压缩文件生成的随机文件名称
        var savename = NewGuid() + "." + ext;
        imageFilter.compress({
            img: imgsrc,
            quality: quality,
            scale: quality,
            save: {
                album: false,
                imgPath: savePath,
                imgName: savename
            }
        }, function (ret, err) {
            if (ret) {
                callback(savePath + savename);
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    // 获取文件拓展名
    function getExt(fileName) {
        return fileName.substring(fileName.lastIndexOf('.') + 1);
    }

    // 获取当前的时间，拼接成2015-11-09这样的格式，主要用于对图片进行时间分类
    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate
        return currentdate;
    }

    // 生成guid,主要用于生成随机文件名
    function NewGuid() {
        function S4() {
            return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
        }

        return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
    }

    function uploadFile(data) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/uploadPhotosAPP";
        var data = {
            values: {
                un: un
            },
            files: {
                file: data
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == "0") {
                    console.log("上传成功");
                } else if (myobj.code == "1") {
                    alert("上传失败");
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }
    //查询详细
    function selectOneSubmitData() {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem('un');
        var url = serverUrl + "/app/getKmManngerSearchDetail";
        var data = {
            values: {
                un: un,
                id: ids
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {
                    var list = eval(myobj.list);
                    $("#informationTime").val(list.informationTime);
                    $("#ext06").val(list.ext06);
                    $("#client").val(list.client);
                    $("#problemOccurs").val(list.problemOccurs);
                    $("#productionUnits").val(list.productionUnits);
                    $("#mrlName").val(list.mrlName);
                    $("#mrlTuhao").val(list.mrlTuhao);
                    $("#mrlStatus").val(list.mrlStatus);
                    $("#lotCode").val(list.lotCode);
                    $("#problemDescription").val(list.problemDescription);
                    $("#problemNum").val(list.problemNum);
                    $("#problemCause").val(list.problemCause);
                    $("#ext03").val(list.ext03);
                    qualityEngineerCode = list.ext14;
                    $("#qualityEngineer").val(list.qualityEngineer);
                    // $("#ext01").val(list.ext01);
                    // ext02 = list.ext02;
                    console.log(list.ext03)
                    //失败
                } else if (myobj.code == "1") {

                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }
    function goSelectExt01() {
        //获取部门
        var ext03 = $("#ext03").val();
        if (ext03 == null || ext03 == "" || ext03 == undefined) {
            api.toast({
                msg: '请选择部门！',
                duration: 3000
            });
            return;
        }
        api.openWin({
            name: 'getDutyPerson',
            url: 'getDutyPerson.html',
            pageParam: {
                name: "qckmmanagesubmitdetail",
                dept: ext03//部门id
            }
        });
    }
    function goSelectMrlName() {
        api.openWin({
            name: 'selectMrlAndTuhao',
            url: 'selectMrlAndTuhao.html',
            pageParam: {
                name: "qckmmanagesubmitdetail",
            }
        });
    }
    function setMrl(sysid, code, name, drawing) {
        console.log(111111)
        console.log(sysid)
        console.log(code)
        console.log(name)
        console.log(drawing)
        $("#mrlName").val(name);
        $("#mrlTuhao").val(drawing);
    }
    function setDutyPerson(sysid, code, name) {
        console.log(1111)
        console.log(name)
        // ext02 = code
        // console.log(ext02)
        // $("#ext01").val(name);
    }
    function doSearchDept() {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem('un');
        var url = serverUrl + "/app/getDeptNameList";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var map = eval("(" + data + ")");
                var list = eval(map.list);
                var html = "<option value=''>--请选择--</option>";
                for (var i = 0; i < list.length; i++) {
                    html += "<option value='" + list[i].sysid + "'>" + list[i].name + "</option>";
                }
                $api.html($api.byId("ext03"), html);
                if (ids) {
                    //查询详细
                    selectOneSubmitData()
                }
            }
        });
    }

    //时间选择器
    function timePicker() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
        api.openPicker({
            type: 'date',
            date: new Date(),
            title: '选择日期'
        }, function (ret, err) {
            if (ret) {
                var monthTemp = (ret.month < 10 ? '0' + ret.month : ret.month);
                var dayTemp = (ret.day < 10 ? '0' + ret.day : ret.day);
                var date = ret.year + "-" + monthTemp + "-" + dayTemp;
                $("#informationTime").val(date);
            }
        });
    }

    //时间选择器
    function timePicker2() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
        api.openPicker({
            type: 'date',
            date: new Date(),
            title: '选择日期'
        }, function (ret, err) {
            if (ret) {
                var monthTemp = (ret.month < 10 ? '0' + ret.month : ret.month);
                var dayTemp = (ret.day < 10 ? '0' + ret.day : ret.day);
                var date = ret.year + "-" + monthTemp + "-" + dayTemp;
                $("#ext06").val(date);
            }
        });
    }

    function changeState(type) {
        var informationTime = $("#informationTime").val();
        var ext06 = $("#ext06").val();
        var client = $("#client").val();
        var problemOccurs = $("#problemOccurs").val();
        var productionUnits = $("#productionUnits").val();
        var mrlName = $("#mrlName").val();
        var mrlTuhao = $("#mrlTuhao").val();
        var mrlStatus = $("#mrlStatus").val();
        var lotCode = $("#lotCode").val();
        var problemDescription = $("#problemDescription").val();
        var problemNum = $("#problemNum").val();
        var problemCause = $("#problemCause").val();
        var ext03 = $("#ext03").val();
        var ext01 = $("#ext01").val();
        var qualityEngineer = $("#qualityEngineer").val();

        var imgs = document.getElementsByTagName('img');
        var names = [];
        for (var i = 0; i < imgs.length; i++) {
            var img = imgs[i];
            var srcStr = img.getAttribute('src');
            var arrs = srcStr.split('\/');
            var nameStr = arrs[arrs.length - 1];
            //alert(nameStr);
            if ("add.jpg" != nameStr) {
                names[i] = nameStr;
            }
        }
        var picNames = JSON.parse(JSON.stringify(names));

        if (informationTime == null || informationTime == "" || informationTime == undefined) {
            api.toast({
                msg: '信息/问题时间不能为空！',
                duration: 3000
            });
            return;
        }

        if (ext06 == null || ext06 == "" || ext06 == undefined) {
            api.toast({
                msg: '截止时间不能为空！',
                duration: 3000
            });
            return;
        }


        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/addDataKmMannger";

        api.showProgress({
            title: '保存中...',
        });

        var data = {
            values: {
                informationTime: informationTime,
                ext06: ext06,
                client: client,
                problemOccurs: problemOccurs,
                productionUnits: productionUnits,
                mrlName: mrlName,
                mrlTuhao: mrlTuhao,
                mrlStatus: mrlStatus,
                lotCode: lotCode,
                problemDescription: problemDescription,
                problemNum: problemNum,
                problemCause: problemCause,
                ext03: ext03,
                picNames: picNames,
                qualityEngineer: qualityEngineer,
                qualityEngineerCode: qualityEngineerCode,
                un: un,
                id: ids
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {

                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });
                    api.closeWin();
                    api.execScript({
                        name: api.pageParam.name,
                        script: 'dealSuccess()'
                    });

                } else if (myobj.code == "1") {

                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //订单返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

<style>
    .c1 {
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(0, 0, 0, .5);
        z-index: 99;
        margin-top: 0%;
    }

    .c2 {
        background-color: white;
        position: fixed;
        width: 400px;
        height: 300px;
        top: 50%;
        left: 50%;
        z-index: 100;
        margin-top: -125px;
        margin-left: -200px;
        padding-top: 2%;
        padding-left: 2%;
        overflow-y: auto;
        display: flex;
        flex-direction: column;
    }

    .hide {
        display: none;
    }
</style>

</html>